package com.zut.test;

import com.github.pagehelper.PageInfo;
import com.zut.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
 * 4、根据mybatis中的关联映射，编写sql
 * 1）、根据订单编号查询用户信息
 * 2）、根据用户名查询用户的订单信息
 * 3）、根据用户名查询用户所购买的所有商品信息
 */
public class UserTest2 {

    private SqlSessionFactory sqlSessionFactory;
   private InputStream inputStream;

    @Before
    public void init() throws Exception{
        inputStream =Resources.getResourceAsStream("mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

  //(2)一对多 、根据用户名查询订单信息
    @Test
    public void testFindUserOrdersByUsername() throws Exception{
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("com.zut.mapper.UserMapper.findUserOrdersByUsername","王");
        for (User u : users) {
            System.out.println( u );
        }
        sqlSession.close();
    }
    
  //(3)根据用户名查询用户所购买的所有商品信息
    @Test
    public void testFindItemsByUsername() throws Exception{
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("com.zut.mapper.UserMapper.findItemsByUsername","王五");
        for (User u : users) {
            System.out.println( u );
        }
        sqlSession.close();
    }

}
